package org.water.common.druid;

import org.water.base.entity.LogBean;
import org.water.common.log.BaseLog;

import com.alibaba.druid.filter.stat.StatFilterContextListenerAdapter;
import com.alibaba.druid.support.spring.stat.SpringMethodStat;

public class CustomSpringMethodContextListener extends StatFilterContextListenerAdapter {
	 @Override
     public void addUpdateCount(int updateCount) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.addJdbcUpdateCount(updateCount);
         }
     }

     @Override
     public void addFetchRowCount(int fetchRowCount) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.addJdbcFetchRowCount(fetchRowCount);
         }
     }

     @Override
     public void executeBefore(String sql, boolean inTransaction) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcExecuteCount();
         }
     }

     @Override
     public void executeAfter(String sql, long nanos, Throwable error) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.addJdbcExecuteTimeNano(nanos);
             if (error != null) {
                 springMethodStat.incrementJdbcExecuteErrorCount();
             }
         }
        
        //记录执行sql
        LogBean logBean=BaseLog.getLogBean(Thread.currentThread().getId());
 		if(logBean!=null)
 		{
 			logBean.setSql(sql);
 			logBean.setSqlExcuteTime(nanos);
 		}
     }

     @Override
     public void commit() {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcCommitCount();
         }
     }

     @Override
     public void rollback() {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcRollbackCount();
         }
     }

     @Override
     public void pool_connect() {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcPoolConnectionOpenCount();
         }
     }

     @Override
     public void pool_close(long nanos) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcPoolConnectionCloseCount();
         }
     }

     @Override
     public void resultSet_open() {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcResultSetOpenCount();
         }
     }

     @Override
     public void resultSet_close(long nanos) {
         SpringMethodStat springMethodStat = SpringMethodStat.current();
         if (springMethodStat != null) {
             springMethodStat.incrementJdbcResultSetCloseCount();
         }
     }
}
